package org.tio.sitexxx.service.service.tiosite;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.cache.CacheConfig;
import org.tio.sitexxx.service.cache.Caches;
import org.tio.sitexxx.service.model.main.Donate;
import org.tio.sitexxx.service.utils.QueryUtils;
import org.tio.sitexxx.service.vo.Const;
import org.tio.utils.cache.ICache;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;

/**
 * @author tanyaowu
 * 2016年7月22日 上午10:44:55
 */
public class DonateService {
	@SuppressWarnings("unused")
	private static Logger				log	= LoggerFactory.getLogger(DonateService.class);
	public static final DonateService	me	= new DonateService();
	private final Donate				dao	= new Donate().dao();

	/**
	 *
	 * @author tanyaowu
	 */
	public DonateService() {
	}

	public Page<Donate> page(int pageNumber, int pageSize) {
		SqlPara sqlPara = dao.getSqlPara("donate.page");
		Page<Donate> ret = dao.paginate(pageNumber, pageSize, sqlPara);
		return ret;
	}

	/**
	 * 捐赠总金额
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 * @author tanyaowu
	 */
	public Double totalAmount() {
		String cacheKey = "donate_totalAmount";
		ICache cache = Caches.getCache(CacheConfig.TIME_TO_LIVE_SECONDS_5);
		Object obj = cache.get(cacheKey);
		if (obj != null) {
			return (Double) obj;
		}

		Double ret = Db.queryDouble("select sum(amount) from donate");
		cache.put(cacheKey, ret);
		return ret;
	}

	public static <V> List<V> randomList(List<V> sourceList) {
		if (sourceList == null || sourceList.size() == 0) {
			return sourceList;
		}
		List<V> random = new ArrayList<V>(sourceList.size());
		do {
			int index = Math.abs(new Random().nextInt(sourceList.size()));
			random.add(sourceList.remove(index));

		} while (sourceList.size() > 0);

		return random;

	}

	static long requestCount = 1;

	/**
	 * count | sum                  | waytype | way
	 * 27	 |  804.31           	|  1	  |  码云
	   21	 |  1068                |  2	  |  微信二维码收款
	   15	 |  1409.43	            |  3	  |  支付宝
	   3	 |  214.88	            |  4	  |  微信红包
	 * 统计捐赠来源总额
	 * @return
	 * @author tanyaowu
	 */
	public List<Record> statAmountComeFrom() {
		String cacheKey = "donate.statAmountComeFrom";
		String sqlid = "donate.statAmountComeFrom";
		String db = Const.Db.TIO_SITE_MAIN;
		CacheConfig cache = CacheConfig.TIME_TO_LIVE_MINUTE_5;
		return QueryUtils.findWithCache(cache, cacheKey, db, sqlid, true);
	}

	/**
	 * count | sum                  | waytype | way
	 * 27	 |  804.31           	|  1	  |  码云
	   21	 |  1068                |  2	  |  微信二维码收款
	   15	 |  1409.43	            |  3	  |  支付宝
	   3	 |  214.88	            |  4	  |  微信红包
	 * 统计捐赠来源次数
	 * @return
	 * @author tanyaowu
	 */
	public List<Record> statCountComeFrom() {
		String cacheKey = "donate.statCountComeFrom";
		String sqlid = "donate.statCountComeFrom";
		String db = Const.Db.TIO_SITE_MAIN;
		CacheConfig cache = CacheConfig.TIME_TO_LIVE_MINUTE_5;
		return QueryUtils.findWithCache(cache, cacheKey, db, sqlid, true);
	}

	/**
	 * 找出捐赠排名前10的用户
	 * @return
	 * @author tanyaowu
	 */
	public List<Record> orderAmountByName() {
		String cacheKey = "donate.orderAmountByName";
		String sqlid = "donate.orderAmountByName";
		String db = Const.Db.TIO_SITE_MAIN;
		CacheConfig cache = CacheConfig.TIME_TO_LIVE_MINUTE_5;
		return QueryUtils.findWithCache(cache, cacheKey, db, sqlid, true);
	}

	/**
	 * 找出捐赠排名前10的用户
	 * @return
	 * @author tanyaowu
	 */
	public List<Record> orderCountByName() {
		String cacheKey = "donate.orderCountByName";
		String sqlid = "donate.orderCountByName";
		String db = Const.Db.TIO_SITE_MAIN;
		CacheConfig cache = CacheConfig.TIME_TO_LIVE_MINUTE_5;
		return QueryUtils.findWithCache(cache, cacheKey, db, sqlid, true);
	}

	/**
	 * 找出最后一笔捐赠时间
	 * @return
	 * @author tanyaowu
	 */
	public Record selectLastDonate() {
		String sqlid = "donate.selectLastDonate";
		String db = Const.Db.TIO_SITE_MAIN;

		SqlPara sqlPara = Db.use(db).getSqlPara(sqlid);
		//		if (params != null) {
		//			for (Object object : params) {
		//				sqlPara.addPara(object);
		//			}
		//		}

		Record record = Db.use(db).findFirst(sqlPara);
		return record;

	}

	//
}
